[Android] SwipeListView

前言

以往都是直接拆開然後自己刻一個,這次學習如何使用第三方套件,環境是Android Studio,先參考在別人的Blog找到的範例。

SwipeListView

SwipeListView

引入

找到build.gradle(app)加入官網指令。

activity_main.xml

一些設定可以到官網看。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.fortysevendeg.swipelistview.SwipeListView
android:id="@+id/example_lv_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
swipe:swipeActionLeft="reveal"
swipe:swipeActionRight="reveal"
swipe:swipeAnimationTime="0"
swipe:swipeBackView="@+id/backview"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeFrontView="@+id/frontview"
swipe:swipeMode="both"
swipe:swipeOffsetLeft="0dp"
swipe:swipeOffsetRight="0dp"
swipe:swipeOpenOnLongPress="false" />
</RelativeLayout>

list.xml

欲顯示的樣子。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/backview"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#eee"
android:tag="back" >
<Button
android:id="@+id/example_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:background="@null" />
<Button
android:id="@+id/edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="編輯" />
<Button
android:id="@+id/del"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="刪除" />
</LinearLayout>
<RelativeLayout
android:id="@+id/frontview"
android:layout_width="match_parent"
android:layout_height="70dp"
android:background="#ffffff"
android:orientation="vertical"
android:tag="front" >
<TextView
android:id="@+id/example_row_tv_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="16sp" />
</RelativeLayout>
</FrameLayout>

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import com.fortysevendeg.swipelistview.SwipeListView;
public class MainActivity extends Activity {
private SwipeListView mSwipeListView ;
private SwipeAdapter mAdapter ;
private List<String> testData ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSwipeListView = (SwipeListView) findViewById(R.id.example_lv_list);
testData = getUserData();
mAdapter = new SwipeAdapter(this, R.layout.list, testData,mSwipeListView);
mSwipeListView.setAdapter(mAdapter);
}
private List<String> getUserData() {
String [] obj = new String[10];
for(int i=0; i<obj.length; i++){
obj[i] = "TEST" + i;
}
List<String> list = new ArrayList<String>(Arrays.asList(obj));
return list;
}
}

SwipeAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import com.fortysevendeg.swipelistview.SwipeListView;
public class SwipeAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater ;
private List<String> objects ;
private SwipeListView mSwipeListView ;
public SwipeAdapter(Context context, int textViewResourceId,List<String> objects, SwipeListView mSwipeListView) {
super(context, textViewResourceId, objects);
this.objects = objects ;
this.mSwipeListView = mSwipeListView ;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null ;
if(convertView == null){
convertView = mInflater.inflate(R.layout.list, parent, false);
holder = new ViewHolder();
holder.mFrontText = (TextView) convertView.findViewById(R.id.example_row_tv_title);
holder.mBackEdit = (Button) convertView.findViewById(R.id.edit);
holder.mBackDelete = (Button) convertView.findViewById(R.id.del);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.mBackDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mSwipeListView.closeAnimate(position);
mSwipeListView.dismiss(position);
}
});
String item = getItem(position);
holder.mFrontText.setText(item);
return convertView;
}
class ViewHolder{
TextView mFrontText ;
Button mBackEdit,mBackDelete ;
}
}

打包

src.rar

參考

Android开源框架之SwipeListView导入及模拟QQ侧滑